openCV uses pass by reference not pythonic pass by value
This code is taken from lesson5 of the openCV tutorial https://pythonprogramming.net/image-arithmetics-logic-python-opencv-tutorial/
import cv2 import numpy as np
# Load two images img1 = cv2.imread('3D-Matplotlib.png') #img1a = img1 img1a = cv2.imread('3D-Matplotlib.png') img2 = cv2.imread('mainlogo.png') img3 = cv2.imread('helloo.png') # I want to put logo on top-left corner, So I create a ROI rows,cols,channels = img2.shape roi = img1[20:rows+20, 20:cols+20]
# Now create a mask of logo img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) # add a threshold ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV) #anything crossing over 220 is thelower limit #binary threshold is 0 or 1 #anything> 220 goes to 255 #anything below 220 goes to 0-> black #and create its inverse mask mask_inv = cv2.bitwise_not(mask) #do same for img3 img3gray = cv2.cvtColor(img3,cv2.COLOR_BGR2GRAY) ret3, mask3 = cv2.threshold(img3gray, 140, 255, cv2.THRESH_BINARY_INV) mask_inv3 = cv2.bitwise_not(mask3)
# take the ROI of the plot, and throw the mask over it img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
# Take only region of logo from logo image. img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
#do the same with the other mask img3_bg = cv2.bitwise_and(roi3,roi3,mask = mask_inv3) img3_fg = cv2.bitwise_and(img3,img3,mask = mask3) #